//
//  ExportOngoingViewController.m
//  Globalstone
//
//  Created by 辰领科技 on 16/7/11.
//  Copyright © 2016年 yuf. All rights reserved.
//

#import "ExportOngoingViewController.h"
#import "ExportTableViewCell.h"
#import "IndentDetailViewController.h"
#import "InIndentDetailViewController.h"

#import "customsInformationViewController.h"
#import "BargeInformationViewController.h"
#import "BillingInfoViewController.h"
#import "BillOfLadingInformationViewController.h"
#import "FreightInformationViewController.h"
#import "CertificationContainerViewController.h"
#import "ShippingInformationViewController.h"

static NSString *cellIdentifier = @"ExportTableViewCell";

@interface ExportOngoingViewController ()
<UITableViewDelegate,UITableViewDataSource>

@property (strong, nonatomic) IBOutlet UIView *navView;
@property (strong, nonatomic) IBOutlet UIScrollView *scroview;
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) IBOutlet UILabel *orderNumberLabel;//单号
@property (strong, nonatomic) IBOutlet UILabel *locationLabel;//当前位置
@property (weak, nonatomic) IBOutlet UIImageView *doneImage;

@property(nonatomic,strong)NSMutableArray * statusArray;
@end

@implementation ExportOngoingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _statusArray = [NSMutableArray array];
    [self initViewLayout];
    if ([_type isEqualToString:@"2"]) {
        [self getOrderExDetail];
        NSArray * array = @[@"已响应",@"拖车信息",@"装柜发货",@"已通关",@"装船海运",@"提单信息",@"已通关"];
        if(_currentStatus>5){
            _currentStatus = 5;
        }
          if (_currentStatus>0) {
        _locationLabel.text = array[_currentStatus-1];
          }
    }else{
        [self getOrderInDetail];

        NSArray * array = @[@"已响应",@"已响应",@"装柜发货",@"开船起运",@"到中转港",@"抵达港口，海关放行",@"抵达港口，海关放行",@"拖车信息",@"货主收货完成",@"到中转港"];
        if(_currentStatus>8){
            _currentStatus = 8;
        }
        if (_currentStatus>0) {
            _locationLabel.text = array[_currentStatus-1];
        }
    }
}

- (void)initViewLayout{
    [self adaptiveViewLayout:self.view];
    [self adaptiveViewLayout:self.navView];
    [self adaptiveViewLayout:self.scroview];
    
    self.scroview.contentSize = flexibleSize(CGSizeMake(320, 505), NO);
    
    [self.tableView registerNib:[UINib nibWithNibName:cellIdentifier bundle:nil] forCellReuseIdentifier:cellIdentifier];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    if ([_indentStatus isEqualToString:@"3"]) {
        _doneImage.image = [UIImage imageNamed:@"CustomsClearanceDetails7"];
    }
    
    _orderNumberLabel.text = _indentNum;
    
   
}

- (IBAction)back:(id)sender {
    [self backBtn];
}

#pragma mark === 进口通关信息
- (void)getOrderInDetail{
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    [dic setObject:[Common getLoginInfo][Key_User_userId] forKey:@"museId"];
    [dic setObject:[Common getLoginInfo][Key_User_password] forKey:@"musePwd"];
    [dic setObject:[Common getLoginInfo][Key_User_onlineKey] forKey:@"museOnlineTag"];
    NSString * lan = [InternationalControl userLanguage];
    if ([lan isEqualToString:@"en"]) {
        [dic setObject:@"2" forKey:@"language"];
    }else{
        [dic setObject:@"1" forKey:@"language"];
    }
     [dic setObject:_indentId forKey:@"oimpId"];
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:GS_queryOrderImportStatusDetail withHttpType:@"POST" withTag:GS_GS_queryOrderImportStatusDetail];
}

#pragma mark === 出口通关信息
- (void)getOrderExDetail{
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    [dic setObject:[Common getLoginInfo][Key_User_userId] forKey:@"museId"];
    [dic setObject:[Common getLoginInfo][Key_User_password] forKey:@"musePwd"];
    [dic setObject:[Common getLoginInfo][Key_User_onlineKey] forKey:@"museOnlineTag"];
    NSString * lan = [InternationalControl userLanguage];
    if ([lan isEqualToString:@"en"]) {
        [dic setObject:@"2" forKey:@"language"];
    }else{
        [dic setObject:@"1" forKey:@"language"];
    }
    [dic setObject:_indentId forKey:@"oexpId"];
    
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:GS_queryOrderExportStatusDetail withHttpType:@"POST" withTag:GS_GS_queryOrderExportStatusDetail];
}

#pragma mark === 订单基本信息
- (IBAction)indentDetail:(id)sender {
    if ([_type isEqualToString:@"1"]) {
        UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"SupplyChainStoryboard" bundle:nil];
        InIndentDetailViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"InIndentDetail"];
        page.indentId = _indentId;
        [[Common getNavigation]pushViewController:page];
    }else if ([_type isEqualToString:@"2"]){
        UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"SupplyChainStoryboard" bundle:nil];
        IndentDetailViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"IndentDetail"];
        page.indentId = _indentId;
        [[Common getNavigation]pushViewController:page];
    }
}


#pragma mark - <UITableViewDelegate,UITableViewDataSource>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _statusArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 45*VerticalRatio();
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ExportTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    if (!cell) {
        cell = [[ExportTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    NSInteger  status ;
    NSDictionary * dic = _statusArray[indexPath.row];
    cell.arrowImage.hidden = NO;
    if ([_type isEqualToString:@"1"]) {
        cell.transInfo.text = [NSString stringWithFormat:@"%@%@",dic[@"oistCreateTime"],dic[@"oistDesc"]];
        status = [[NSString stringWithFormat:@"%@",dic[@"oistStatus"]]integerValue];
        if (status == 7) {//拖车信息
            cell.transInfo.text = [NSString stringWithFormat:@"%@ %@",dic[@"oistCreateTime"],@"拖车信息"];
        }else if (status == 3){//装柜发货
            cell.transInfo.text = [NSString stringWithFormat:@"%@ %@",dic[@"oistCreateTime"],@"装柜发货"];
        }else if (status == 4){//海运信息
            cell.transInfo.text = [NSString stringWithFormat:@"%@ %@",dic[@"oistCreateTime"],@"开船起运"];
        }else if (status == 9){//中转港驳船
            cell.transInfo.text = [NSString stringWithFormat:@"%@ %@",dic[@"oistCreateTime"],@"到中转港，驳船起运"];
        }else if (status == 5){//抵达港口海关
            cell.transInfo.text = [NSString stringWithFormat:@"%@ %@",dic[@"oistCreateTime"],@"抵达港口，等待海关放行"];
        }else if (status == 6){//抵达港口海关
            cell.transInfo.text = [NSString stringWithFormat:@"%@ %@",dic[@"oistCreateTime"],@"抵达港口，海关放行"];
        }else if (status == 0){//结算
            cell.transInfo.text = [NSString stringWithFormat:@"%@ %@",dic[@"oistCreateTime"],@"结算信息"];
        }else if (status == 8){//收货
            cell.transInfo.text = [NSString stringWithFormat:@"%@ %@",dic[@"oistCreateTime"],@"货主收货完成"];
            cell.arrowImage.hidden = YES;
        }else if (status == 1){//代订舱
            cell.transInfo.text = [NSString stringWithFormat:@"%@ %@",dic[@"oistCreateTime"],@"待订仓"];
            cell.arrowImage.hidden = YES;
        }else if (status == 2){//待装柜
            cell.transInfo.text = [NSString stringWithFormat:@"%@ %@",dic[@"oistCreateTime"],@"待装柜"];
            cell.arrowImage.hidden = YES;
        }
        
        NSArray * statuss = @[@"0",@"3",@"4",@"5",@"6",@"7",@"9"];
        cell.arrowImage.hidden = YES;
        if ([statuss containsObject:[NSString stringWithFormat:@"%ld",status]]) {
            cell.arrowImage.hidden = NO;
        }
    }else{
      cell.transInfo.text = [NSString stringWithFormat:@"%@%@",dic[@"oestCreateTime"],dic[@"oestDesc"]];
        status = [[NSString stringWithFormat:@"%@",dic[@"oestStatus"]]integerValue];
        if (status == 2) {//拖车信息
            cell.transInfo.text = [NSString stringWithFormat:@"%@ %@",dic[@"oestCreateTime"],@"拖车信息"];
        }else if (status == 3){//装柜发货
            cell.transInfo.text = [NSString stringWithFormat:@"%@ %@",dic[@"oestCreateTime"],@"装柜发货"];
        }else if (status == 4){//开船启运
            cell.transInfo.text = [NSString stringWithFormat:@"%@ %@",dic[@"oestCreateTime"],@"装船海运"];
        }else if (status == 5){//提单信息
            cell.transInfo.text = [NSString stringWithFormat:@"%@ %@",dic[@"oestCreateTime"],@"提单信息"];
        }else if (status == 6){//提单信息
            cell.transInfo.text = [NSString stringWithFormat:@"%@ %@",dic[@"oestCreateTime"],@"已通关"];
            cell.arrowImage.hidden = YES;
        }else if (status == 0){//提单信息
            cell.transInfo.text = [NSString stringWithFormat:@"%@ %@",dic[@"oestCreateTime"],@"退税完成"];
            cell.arrowImage.hidden = YES;
        }

        NSArray * statuss = @[@"2",@"3",@"4",@"5"];
        cell.arrowImage.hidden = YES;
        if ([statuss containsObject:[NSString stringWithFormat:@"%ld",status]]) {
            cell.arrowImage.hidden = NO;
        }
    }
    
    
    
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSDictionary * dic = _statusArray[indexPath.row];
    
    NSInteger  status ;
    NSString * indentId = _indentId ;
    if ([_type isEqualToString:@"1"]) {//进口
       status = [[NSString stringWithFormat:@"%@",dic[@"oistStatus"]]integerValue];
        if (status == 7) {//拖车信息
            UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"SupplyChainStoryboard" bundle:nil];
            FreightInformationViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"FreightInformation"];
            page.indentId = indentId;
            page.type = _type;
            [[Common getNavigation]pushViewController:page];
        }else if (status == 3){//装柜发货
            UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"SupplyChainStoryboard" bundle:nil];
            CertificationContainerViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"Certification"];
            page.indentId = indentId;
            page.type = _type;
            [[Common getNavigation]pushViewController:page];
        }else if (status == 4){//海运信息
            UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"SupplyChainStoryboard" bundle:nil];
            ShippingInformationViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"ShippingInformation"];
            page.indentId = indentId;
            page.type = _type;
            [[Common getNavigation]pushViewController:page];
        }else if (status == 9){//中转港驳船
            UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"SupplyChainStoryboard" bundle:nil];
            BargeInformationViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"BargeInformation"];
            page.indentId = indentId;
            [[Common getNavigation]pushViewController:page];
        }else if (status == 5||status == 6){//抵达港口海关
            UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"SupplyChainStoryboard" bundle:nil];
            customsInformationViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"customsInformation"];
            page.indentId = indentId;
            [[Common getNavigation]pushViewController:page];
        }else if (status == 0){//结算
            UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"SupplyChainStoryboard" bundle:nil];
            BillingInfoViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"BillingInfo"];
            page.indentId = indentId;
            [[Common getNavigation]pushViewController:page];
        }
    }else{//出口
        status = [[NSString stringWithFormat:@"%@",dic[@"oestStatus"]]integerValue];
        if (status == 2) {//拖车信息
            UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"SupplyChainStoryboard" bundle:nil];
            FreightInformationViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"FreightInformation"];
            page.indentId = indentId;
            page.type = _type;
            [[Common getNavigation]pushViewController:page];
        }else if (status == 3){//装柜发货
            UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"SupplyChainStoryboard" bundle:nil];
            CertificationContainerViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"Certification"];
            page.indentId = indentId;
            page.type = _type;
            [[Common getNavigation]pushViewController:page];
        }else if (status == 4){//开船启运
            UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"SupplyChainStoryboard" bundle:nil];
            ShippingInformationViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"ShippingInformation"];
            page.indentId = indentId;
            page.type = _type;
            [[Common getNavigation]pushViewController:page];
        }else if (status == 5){//提单信息
            UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"SupplyChainStoryboard" bundle:nil];
            BillOfLadingInformationViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"BillOfLadingInformation"];
            page.indentId = indentId;
            [[Common getNavigation]pushViewController:page];
        }
        
    }
    
}

#pragma   mark---- GetADBanner Request Delegate

-(void)fetchDatabaseFinished:(NSMutableDictionary *)database withTag:(NSInteger)tag{
    
    if (tag==GS_GS_queryOrderExportStatusDetail||tag == GS_GS_queryOrderImportStatusDetail) {
        
        if (  [[database valueForKey:@"flag"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            if ([database[@"result"]isEqual:[NSNull null]]) {
                return;
            }
            if ([_type isEqualToString:@"1"]) {
                _statusArray = database[@"result"][@"OrderImportStatusList"];
            }else{
            _statusArray = database[@"result"][@"ExportStatusList"];
            }
            
            [_tableView reloadData];
        }
    }
}

-(void)fetchDatabaseFailed:(NSError *)error message:(NSString *)message{
        
        [self dismissWaitDialog];
        
}


@end
